# include "Personage.h"

# include "Renderer.h"

Personage::Personage() : mySceneNode( 0), myBody( 0), myDirection( Ogre::Vector3::ZERO), myDistance( -0.1f), myBodyNode( 0)
{
	myActive = true;
}

Personage::~Personage()
{
	if ( myBody )
	{
		myRenderer->deleteEntity( myBody);
	}

	if ( myBodyNode )
	{
		myRenderer->deleteSceneNode( myBodyNode);
	}

	if ( mySceneNode )
	{
		myRenderer->deleteSceneNode( mySceneNode);
	}
}

void Personage::setName( const Ogre::String& name)
{
	myName = name;
}

void Personage::setLevel( unsigned int level)
{
	myLevel = level;
}

void Personage::setSize( Ogre::Real size)
{
	mySize = size;
}

void Personage::setPosition( Ogre::Vector3 point)
{
	mySceneNode->setPosition( point);
}

void Personage::setDirection( Ogre::Vector3 direction)
{
	myDirection = direction;
	myDistance = myDirection.normalise();

	Ogre::Vector3 srcTmp = myBodyNode->getOrientation() * -Ogre::Vector3::UNIT_Z;
	
	if ( ( 1.0f + srcTmp.dotProduct( myDirection) ) < 0.0001f )
	{
		myBodyNode->yaw( Ogre::Degree( 180));
	}

	else 
	{
		Ogre::Quaternion quatTmp = srcTmp.getRotationTo( myDirection);
		myBodyNode->rotate( quatTmp);
	}
}

void Personage::addDirection( const Ogre::Vector3& direction, Ogre::Real distance)
{
	myDirections.push( direction);
	myDistances.push( distance);
}

int Personage::getCurrentLife()
{
	return myLifeCurrent;
}

int Personage::getLife()
{
	return myLife;
}

Ogre::Vector3 Personage::getPosition()
{
	return mySceneNode->getPosition();
}

Ogre::Real Personage::getSize()
{
	return mySize;
}

Ogre::String Personage::getNodeName()
{
	return mySceneNode->getName();
}

const std::string& Personage::getName()
{
	return myName;
}

const Ogre::String Personage::getFullName()
{
	std::ostringstream strTmp;
	strTmp << myName << " lvl " << myLevel;

	return strTmp.str();
}

void Personage::receiveDamage( const Damage& damage)
{
	myLifeCurrent -= damage.damage;

	if ( myLifeCurrent <= 0 )
	{
		myActive = false;
		myBody->setQueryFlags( DEATH_FLAG);
	}
}

void Personage::clearPath()
{
	while ( myDirections.size() )
	{
		myDirections.pop();
		myDistances.pop();
	}
}

void Personage::init( Renderer* renderer)
{
	myRenderer = renderer;

	mySpeed = 300.0f;
	myTimeAction = 0.0f;
}